package cn.wolfcode.designpattern.factory.abstractfactory;

import java.sql.PreparedStatement;

public class AbstractFactoryTest {

    public static void main(String[] args) {
        PreparedStatement
        System.out.println("------ 支付测试开始 -------");
        PayController payController = new PayController();
        payController.setApiFactory(new WebAliFactory());
        payController.pay();
        System.out.println("------ 支付测试结束 -------");

        System.out.println();

        System.out.println("------ 人脸识别测试开始 -------");
        FaceDetectController faceDetectController = new FaceDetectController();
        faceDetectController.setApiFactory(new WebAliFactory());
        faceDetectController.faceDetect();
        System.out.println("------ 人脸识别测试结束 -------");
    }
}

/**
 * 抽象的支付对象
 */
abstract class Pay {
    protected String name;

    abstract void build();

    public String signature() {
        System.out.println(name + " 正创建支付签名");
        return "签名";
    }

    public boolean verifySignature() {
        System.out.println(name + " 在验证支付签名");
        return true;
    }

    public void payment() {
        System.out.println(name + " 开始支付了");
    }

    public void setName(String name) {
        this.name = name;
    }
}

/**
 * 抽象的人脸识别对象
 */
abstract class FaceDetect {
    protected String name;

    abstract void build();

    public String signature() {
        System.out.println(name + " 正在创建人脸识别签名");
        return "签名";
    }

    public boolean verifySignature() {
        System.out.println(name + " 在验证人脸识别签名");
        return true;
    }

    public void faceDetect() {
        System.out.println(name + " 正在进行人脸识别");
    }

    public void setName(String name) {
        this.name = name;
    }
}

/**
 * 阿里web支付对象
 */
class AliWebPay extends Pay {

    public AliWebPay() {
        setName("阿里web支付");
    }

    @Override
    public void build() {
        System.out.println("构建支付宝WEB支付环境");
    }
}

/**
 * 阿里人脸识别对象
 */
class AliFaceDetect extends FaceDetect {

    public AliFaceDetect() {
        setName("阿里人脸识别");
    }

    @Override
    public void build() {
        System.out.println("构建支付宝人脸识别环境");
    }
}

/**
 * 微信的web支付对象
 */
class WechatWebPay extends Pay {

    @Override
    public void build() {
        System.out.println("构建微信WEB支付环境");
    }
}

/**
 * 微信的人脸识别对象
 */
class WechatFaceDetect extends FaceDetect {

    @Override
    public void build() {
        System.out.println("构建微信人脸识别环境");
    }
}

/**
 * 云api抽象工厂
 */
abstract class AbstractCloudApiFactory {

    abstract public Pay createPay();

    abstract public FaceDetect createFaceDetect();
}

/**
 * 阿里工厂类
 */
class WebAliFactory extends AbstractCloudApiFactory {

    public Pay createPay() {
        return new AliWebPay();
    }

    public FaceDetect createFaceDetect() {
        return new AliFaceDetect();
    }
}

/**
 * 微信工厂类
 */
class WebWechatFactory extends AbstractCloudApiFactory {

    public Pay createPay() {
        return new WechatWebPay();
    }

    public FaceDetect createFaceDetect() {
        return new WechatFaceDetect();
    }
}

/**
 * 支付的消费方
 */
class PayController {
    /**
     * 依赖抽象工厂
     */
    private AbstractCloudApiFactory apiFactory;

    /**
     * 注入对应的子类工厂
     *
     * @param apiFactory
     */
    public void setApiFactory(AbstractCloudApiFactory apiFactory) {
        this.apiFactory = apiFactory;
    }

    public void pay() {
        Pay pay = apiFactory.createPay();

        pay.build();
        pay.signature();
        pay.verifySignature();
        pay.payment();
    }
}

/**
 * 人脸识别的消费方
 */
class FaceDetectController {
    private AbstractCloudApiFactory apiFactory;

    public void setApiFactory(AbstractCloudApiFactory apiFactory) {
        this.apiFactory = apiFactory;
    }

    public void faceDetect() {
        FaceDetect fd = apiFactory.createFaceDetect();

        fd.build();
        fd.signature();
        fd.verifySignature();
        fd.faceDetect();
    }
}
